var plugin = requirePlugin("WechatSI")

var innerAudioContext = wx.createInnerAudioContext();
innerAudioContext.onError((res) => {
  // 播放音频失败的回调
  uni.showToast({
  	title: '语音播放失败',
	icon: 'none'
  })
})
function playTTS(text) {
  //need to add WXAPP plug-in unit: WechatSI
  plugin.textToSpeech({
    lang: "zh_CN",
    tts: true,
    content: text,
    success: function (res) {
      console.log("succ tts", res.filename)
      innerAudioContext.src = res.filename;
      innerAudioContext.play()
    },
    fail: function (res) {
      console.log("fail tts", res)
    }
  })
}

function stopTTS() {
  innerAudioContext.stop();
  // console.log('播放已停止')
}

module.exports = {
  playTTS: playTTS,
  stopTTS: stopTTS,
  ttsPlayer: innerAudioContext
}